<template>
  <el-dialog
    :close-on-click-modal="false"
    :visible="dialogVisible"
    :title="'扫描清单详情 ' + scanFileName"
    append-to-body
    width="90%"
    top="5vh"
    @close="$emit('close')"
  >
    <!--工具栏-->
    <div class="crud-group">
      <!-- 搜索 -->
      <el-input
        v-model="query.scanItemDescribe"
        clearable
        size="small"
        placeholder="检查项"
        style="width:200px"
        class="filter-item"
      />
      <rrOperation :crud="crud" />
    </div>
    <!--表格渲染-->
    <el-table
      ref="table"
      v-loading="crud.loading"
      :data="crud.data"
      border
      max-height="500px"
      :span-method="dataSpanMethod"
      @select="crud.selectChange"
      @select-all="crud.selectAllChange"
      @selection-change="crud.selectionChangeHandler"
    >
      <el-table-column
        label="扫描代码块编号"
        prop="scanSqlIndex"
      />
      <el-table-column
        label="扫描项"
        prop="scanItemDescribe"
      >
        <template
          slot-scope="scope"
        >
          <el-tooltip
            v-if="scope.row.scanDescInfo"
            class="item"
            effect="dark"
            :content="scope.row.scanDescInfo"
            placement="top"
          >
            <span class="preserve-whitespace">{{ scope.row.scanItemDescribe }}</span>
          </el-tooltip>

          <span v-else class="preserve-whitespace">
            {{ scope.row.scanItemDescribe }}
          </span>
        </template>
      </el-table-column>
      <el-table-column
        label="扫描状态"
        prop="scanState"
      >
        <template slot-scope="scope">
          <el-tag
            v-if="scope.row.scanState==='success'"
            size="small"
            type="success"
          >成功</el-tag>
          <el-tag
            v-if="scope.row.scanState==='scanning'"
            size="small"
          >扫描中</el-tag>
          <el-tag
            v-if="scope.row.scanState==='warning'"
            size="small"
            type="warning"
          >警告</el-tag>
          <el-tag
            v-if="scope.row.scanState==='default'"
            size="small"
            type="info"
          >代码块未涉及</el-tag>
          <el-tag
            v-if="scope.row.scanState==='ignore'"
            size="small"
          >忽略警告</el-tag>
          <el-tag
            v-if="scope.row.scanState==='fail'"
            size="small"
            type="danger"
          >异常</el-tag>
          <el-tag
            v-if="scope.row.scanState==='antlrFail'"
            size="small"
            type="danger"
          >代码块解析异常</el-tag>
          <el-tag
            v-if="scope.row.scanState==='badSql'"
            size="small"
            type="danger"
          >SQL语法异常</el-tag>
          <el-tag
            v-if="scope.row.scanState==='error'"
            size="small"
            type="danger"
          >校验出现错误</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="在线预览">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="lookSql(scope.row)"
          >在线预览</el-button>
        </template>
      </el-table-column>
      <el-table-column label="异常忽略">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.scanState==='warning'"
            type="text"
            @click="ignoreWarning(scope.row)"
          >忽略</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <!--分页组件-->
      <pagination />
    </div>

    <el-dialog
      append-to-body
      :close-on-click-modal="false"
      :visible.sync="showScanSqlFlag"
      title="扫描语句预览"
      width="800px"
      top="12vh"
    >
      <monaco-editor
        ref="convertMonaco"
        v-model="sqlContent"
        style="height: 430px"
      />
    </el-dialog>
  </el-dialog>
</template>

<script>
import CRUD, {presenter, header, form, crud} from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import pagination from '@crud/Pagination'
import scriptPackScanResultApi from '@/api/script/pack/scriptPackScanResult'
import MonacoEditor from '@/components/MonacoEditor/monacoEditorSql'
// import codemirror from '@/components/codemirror/codemirror'

// crud交由presenter持有
const defaultCrud = CRUD({title: '脚本扫描清单', url: 'api/scriptPack/scanResult/page', crudMethod: { ...scriptPackScanResultApi }})
const defaultForm = {}
export default {
  name: 'ScriptScanDetail',
  components: {rrOperation, pagination, MonacoEditor},
  props: {
    scanListId: {
      type: String,
      default: null
    },
    dialogVisible: {
      type: Boolean,
      default: false
    },
    scanFileName: {
      type: String,
      default: ''
    }
  },
  watch: {
    dialogVisible(val) {
      if (val) {
        this.page = 0
        this.crud.resetQuery()
      }
    }
  },
  mixins: [presenter(defaultCrud), header(), form(defaultForm), crud()],
  data() {
    return {
      indexArray: [],
      tableData: [],
      showScanSqlFlag: false,
      sqlContent: null
    }
  },
  created() {
    this.crud.page.size = 100
  },
  methods: {
    [CRUD.HOOK.beforeRefresh](crud) {
      crud.query.scanListId = this.scanListId
    },
    // 合并单元格-START
    [CRUD.HOOK.afterRefresh](crud) {
      this.indexArray = []
      this.tableData = crud.data
      let count = 0
      for (let rowIndex = 0; rowIndex < this.tableData.length;) {
        this.indexArray.push(rowIndex)
        count = this.getRows(rowIndex, this.tableData[rowIndex].scanSqlIndex)
        rowIndex += count
      }
    },
    getRows(rowIndex, scanSqlIndex) {
      let count = 0
      for (let i = rowIndex; i < this.tableData.length; i++) {
        if (this.tableData[i].scanSqlIndex === scanSqlIndex) {
          count++
        } else {
          break
        }
      }
      return count
    },
    dataSpanMethod({ row, column, rowIndex, columnIndex }) {
      if (columnIndex === 0) {
        let rowCount = 0
        if (this.indexArray.includes(rowIndex)) {
          rowCount = this.getRows(rowIndex, row.scanSqlIndex)
          return { rowspan: rowCount, colspan: 1 }
        } else {
          return { rowspan: 0, colspan: 0 }
        }
      } else if (columnIndex === 3) {
        let rowCount = 0
        if (this.indexArray.includes(rowIndex)) {
          rowCount = this.getRows(rowIndex, row.scanSqlIndex)
          return { rowspan: rowCount, colspan: 1 }
        } else {
          return { rowspan: 0, colspan: 0 }
        }
      }
    },
    // 合并单元格-END
    ignoreWarning(row) {
      scriptPackScanResultApi.ignoreById(row.scanListId, row.id).then(res => {
        this.$notify({ title: '成功', message: '忽略成功！', type: 'success' })
        this.crud.refresh()
      })
    },
    lookSql(data) {
      this.showScanSqlFlag = true
      this.sqlContent = data.scanSql === null ? '' : data.scanSql
    }
  }
}
</script>
<style scoped>
.preserve-whitespace {
  white-space: pre-wrap; /* 保留空白符，包括换行 */
  word-break: break-all; /* 确保长单词也能正确换行 */
}
</style>
